package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.MainActivity;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.*;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;
import com.tiza.xiaoyoubao.utils.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by mapple on 17/7/27.
 * 取消任务
 */

public class CancleTaskFragment extends BaseFragment {

    @BindView(R.id.tv_reason1)
    TextView tv_reason1;
    @BindView(R.id.tv_reason2)
    TextView tv_reason2;
    @BindView(R.id.tv_reason3)
    TextView tv_reason3;
    @BindView(R.id.tv_reason4)
    TextView tv_reason4;
    @BindView(R.id.iv_check1)
    ImageView iv_check1;
    @BindView(R.id.iv_check2)
    ImageView iv_check2;
    @BindView(R.id.iv_check3)
    ImageView iv_check3;
    @BindView(R.id.iv_check4)
    ImageView iv_check4;
    private TaskOperateInfo taskOperateInfo;
    private BrowseOperateInfo browseOperateInfo;
    private BrowseConfirmInfo browseConfirmInfo;
    private TbTaskOperateInfo mTbTaskOperateInfo;
    InsertTaskInfo mInsertTaskInfo = Hawk.get(Constants.KEY_INSERTTASK_INFO, (InsertTaskInfo) null);
    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);

    Bundle args;
    private String way, cid, orderid, addtime, buyerid, taskCode, reason, account, sellerID, cancleTime;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_cancle_task;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        args = getArguments();
        way = args.getString("way");
        buyerid = userInfo.getWebzzCmsBuyerInfo().getUserid();
        Log.i("cancelTask", way);
        if (way.equals("1")) {
            cid = String.valueOf(mInsertTaskInfo.getCid());
            orderid = mInsertTaskInfo.getOrderid();
            taskCode = mInsertTaskInfo.getTaskCode();
            account = mInsertTaskInfo.getAccount();
            sellerID = mInsertTaskInfo.getSellerid();
            addtime = DateUtils.getDateToStringSec(mInsertTaskInfo.getAddtime());
        } else if (way.equals("2")) {
            taskOperateInfo = (TaskOperateInfo) args.getSerializable("info");
            cid = String.valueOf(taskOperateInfo.getId());
            orderid = taskOperateInfo.getOrderid();
            taskCode = taskOperateInfo.getTaskCode();
            account = taskOperateInfo.getAccount();
            sellerID = taskOperateInfo.getSellerId();
            addtime = DateUtils.getDateToStringSec(taskOperateInfo.getAddTime());
        } else if (way.equals("3")) {
            //浏览待操作
            browseOperateInfo = (BrowseOperateInfo) args.getSerializable("info");
            cid = String.valueOf(browseOperateInfo.getId());
            orderid = browseOperateInfo.getOrderid();
            taskCode = browseOperateInfo.getTaskCode();
            account = browseOperateInfo.getAccount();
            sellerID = browseOperateInfo.getSellerId();
            addtime = DateUtils.getDateToStringSec(browseOperateInfo.getAddTime());
        } else if (way.equals("4")) {
            //浏览待确认
            browseConfirmInfo = (BrowseConfirmInfo) args.getSerializable("info");
            cid = String.valueOf(browseConfirmInfo.getId());
            orderid = browseConfirmInfo.getOrderid();
            taskCode = browseConfirmInfo.getTaskCode();
            account = browseConfirmInfo.getAccount();
            sellerID = browseConfirmInfo.getSellerId();
            addtime = DateUtils.getDateToStringSec(browseConfirmInfo.getAddTime());
        } else if (way.equals("5")) {
            cid = String.valueOf(mInsertTaskInfo.getCid());
            orderid = mInsertTaskInfo.getOrderid();
            taskCode = mInsertTaskInfo.getTaskCode();
            account = mInsertTaskInfo.getAccount();
            sellerID = mInsertTaskInfo.getSellerid();
            addtime = DateUtils.getDateToStringSec(mInsertTaskInfo.getAddtime());
        } else if (way.equals("6")) {
            mTbTaskOperateInfo = (TbTaskOperateInfo) args.getSerializable("info");
            cid = String.valueOf(mTbTaskOperateInfo.getId());
            orderid = mTbTaskOperateInfo.getOrderid();
            taskCode = mTbTaskOperateInfo.getTaskCode();
            account = mTbTaskOperateInfo.getAccount();
            sellerID = mTbTaskOperateInfo.getSellerId();
            addtime = DateUtils.getDateToStringSec(mTbTaskOperateInfo.getAddTime());
        }
        cancleTime = sdf.format(new Date());
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @OnClick({
            R.id.bt_no,
            R.id.bt_yes,
            R.id.view_check1,
            R.id.view_check2,
            R.id.view_check3,
            R.id.view_check4
    })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_no:
                finish();
                break;
            case R.id.bt_yes:
                if (TextUtils.isEmpty(reason)) {
                    toast("请选择取消原因");
                    return;
                } else {
                    Log.i("cancelTask", way);
                    boolean browseFlag = "3".equals(way) || "4".equals(way) || "5".equals(way);
                    if ("1".equals(way) || "2".equals(way) || "6".equals(way)) {
                        cancelTask("001");
                    } else if (browseFlag) {
                        cancelTask("002");
                    }
                }
                break;
            case R.id.view_check1:
                reason = tv_reason1.getText().toString();
                iv_check1.setBackgroundResource(R.mipmap.check_yes);
                iv_check2.setBackgroundResource(R.mipmap.check_no);
                iv_check3.setBackgroundResource(R.mipmap.check_no);
                iv_check4.setBackgroundResource(R.mipmap.check_no);
                break;
            case R.id.view_check2:
                reason = tv_reason2.getText().toString();
                iv_check1.setBackgroundResource(R.mipmap.check_no);
                iv_check2.setBackgroundResource(R.mipmap.check_yes);
                iv_check3.setBackgroundResource(R.mipmap.check_no);
                iv_check4.setBackgroundResource(R.mipmap.check_no);
                break;
            case R.id.view_check3:
                reason = tv_reason3.getText().toString();
                iv_check1.setBackgroundResource(R.mipmap.check_no);
                iv_check2.setBackgroundResource(R.mipmap.check_no);
                iv_check3.setBackgroundResource(R.mipmap.check_yes);
                iv_check4.setBackgroundResource(R.mipmap.check_no);
                break;
            case R.id.view_check4:
                reason = tv_reason4.getText().toString();
                iv_check1.setBackgroundResource(R.mipmap.check_no);
                iv_check2.setBackgroundResource(R.mipmap.check_no);
                iv_check3.setBackgroundResource(R.mipmap.check_no);
                iv_check4.setBackgroundResource(R.mipmap.check_yes);
                break;
            default:
                break;
        }

    }


    private void cancelTask(String typePid) {
        ApiHttpClient.cancelTak(this, cid, orderid, taskCode, cancleTime, reason, buyerid, typePid,
                                account, sellerID, "0",
                                new JsonCallback<String>(String.class, true) {

                                    @Override
                                    public void onBefore(Request request) {
                                        super.onBefore(request);
                                    }

                                    @Override
                                    public void onError(Call call, Exception e) {
                                        toast("网络连接失败,请稍后再试");
                                    }

                                    @Override
                                    public void onResponse(ResponseInfo<String> response) {
                                        int code = response.getCode();
                                        String message = response.getMessage();
                                        if (code == 1) {
                                            showWarnDialog(message);
                                            return;
                                        }
                                        Map<String, Object> _map = response.getFirstEntity();
                                        UIHelper.show(getActivity(), MainActivity.class, true);
                                        finish();
                                    }

                                    @Override
                                    public void onAfter() {
                                        super.onAfter();
                                    }
                                }
        );
    }

}
